package com.shujia.flinkcore;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

public class StudentsWindow {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setBufferTimeout(0);
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        FileSource<String> src = FileSource
                .forRecordStreamFormat(
                        new TextLineInputFormat("UTF-8"),
                        new Path("spark/data/students.csv")
                )
                .build();
        DataStreamSource<String> stuDS = env.fromSource(src, WatermarkStrategy.noWatermarks(), "students");

        SingleOutputStreamOperator<Tuple2<String, Integer>> kv = stuDS.map(line -> Tuple2.of(line.split(",")[4], 1), Types.TUPLE(Types.STRING, Types.INT));

        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = kv
                .keyBy(key -> key.f0)
                .window(EventTimeSessionWindows.withGap(Time.minutes(10))).sum(1);
        sum.print();
        env.execute();
    }
}
